package com.example.aop.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
public class TimeRecordAspect {
    /**
     * 记录方法执行时间
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around("@annotation(com.example.aop.aspect.TimeRecord)") // 作用于所有加 TimeRecord 的方法
//    @Around("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
    public Object timeRecord(ProceedingJoinPoint joinPoint) throws Throwable {
        /**
         * 1. 记录开始时间
         * 2. 执行目标方法
         * 3. 记录结束时间
         * 4. 记录耗时
         */
        long start = System.currentTimeMillis();
        // 调用目标方法
        Object proceed = joinPoint.proceed();
        long end = System.currentTimeMillis();
        log.info(joinPoint.getSignature() + "耗时：{} ms", end - start);
        return proceed;
    }
}
